﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;


using MusicPlayerLib.DataModel;

namespace MusicPlayerLib.ViewModel
{
    /// <summary>
    /// 该类是绑定到PlaylistControl的ViewModel
    /// 在初始化之后一个该类的实例会传递给MusicPlayer,并且会被绑定到
    /// UI上;
    /// </summary> 
    public class PlaylistViewModel:INotifyPropertyChanged
    {
        #region filed

        private string title;
        private int cIndex;
        ObservableCollection<AudioFile>  tracks;
        #endregion

        public event PropertyChangedEventHandler  PropertyChanged;

        #region Constructor
        public PlaylistViewModel()
        {
            this.title = "Default";
            this.tracks = new ObservableCollection<AudioFile>();
            this.cIndex = 0;
        }

        public PlaylistViewModel(Playlist list)
        {
            title = list.Title;
            tracks = new ObservableCollection<AudioFile>();
            //将plylist中的加入到trakcs中;

            foreach (AudioFile af in list.Tracks)
            {
                tracks.Add(af);
            }
            cIndex = 0;
        }
        #endregion


        #region Property
        public string Title
        {
            get
            {
                return title;
            }
            set
            {
                if (!(title.Equals(value)))
                {
                    title = value;
                    this.Notify("Title");
                }
            }
        }

        public int CIndex
        {
            get
            {
                return cIndex;
            }
            set
            {
                if (value < 0 || value >= tracks.Count)
                    return;
                cIndex = value;
                this.Notify("CIndex");
            }
        }

        public ObservableCollection<AudioFile> Tracks
        {
            get
            {
                return tracks;
            }
            set
            {
                tracks = value;
            }
        }
        #endregion

        private void Notify(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        /// <summary>
        /// 获得当前的曲目，如果list为空的话返回null
        /// </summary>
        /// <returns></returns>
        public AudioFile GetCurrentTrack()
        {
            if (cIndex < tracks.Count)
                return tracks[cIndex];
            else
                return null;
        }

        /// <summary>
        /// 移动到前一首个;
        /// </summary>
        public void MoveToPrevious()
        {
            if (tracks.Count < 2 || cIndex == 0)
                return;
            else
            {
                cIndex -= 1;
            }
        }
        /// <summary>
        /// 移动到下一首歌;
        /// </summary>
        public void MoveToNext()
        {
            if(tracks.Count<2||cIndex==(tracks.Count-1))
                return;
            else
            {
                cIndex+=1;
            }
        }
    }
}
